home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / image / imagef~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  6.1 KB  |  173 lines

  1. /*
  2.  * @(#)ImageFilter.java    1.14 95/12/14 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. import java.util.Hashtable;
  23.  
  24. /**
  25.  * This class implements a filter for the set of interface methods that
  26.  * are used to deliver data from an ImageProducer to an ImageConsumer.
  27.  * It is meant to be used in conjunction with a FilteredImageSource
  28.  * object to produce filtered versions of existing images.  It is a
  29.  * base class that provides the calls needed to implement a "Null filter"
  30.  * which has no effect on the data being passed through.  Filters should
  31.  * subclass this class and override the methods which deal with the
  32.  * data that needs to be filtered and modify it as necessary.
  33.  *
  34.  * @see FilteredImageSource
  35.  * @see ImageConsumer
  36.  *
  37.  * @version    1.14 12/14/95
  38.  * @author     Jim Graham
  39.  */
  40. public class ImageFilter implements ImageConsumer, Cloneable {
  41.     /**
  42.      * The consumer of the particular image data stream for which this
  43.      * instance of the ImageFilter is filtering data.  It is not
  44.      * initialized during the constructor, but rather during the
  45.      * getFilterInstance() method call when the FilteredImageSource
  46.      * is creating a unique instance of this object for a particular
  47.      * image data stream.
  48.      * @see #getFilterInstance
  49.      * @see ImageConsumer
  50.      */
  51.     protected ImageConsumer consumer;
  52.  
  53.     /**
  54.      * Returns a unique instance of an ImageFilter object which will
  55.      * actually perform the filtering for the specified ImageConsumer.
  56.      * The default implementation just clones this object.
  57.      */
  58.     public ImageFilter getFilterInstance(ImageConsumer ic) {
  59.     ImageFilter instance = (ImageFilter) clone();
  60.     instance.consumer = ic;
  61.     return instance;
  62.     }
  63.  
  64.     /**
  65.      * Filters the information provided in the setDimensions method
  66.      * of the ImageConsumer interface.
  67.      * @see ImageConsumer#setDimensions
  68.      */
  69.     public void setDimensions(int width, int height) {
  70.     consumer.setDimensions(width, height);
  71.     }
  72.  
  73.     /**
  74.      * Passes the properties from the source object along after adding a
  75.      * property indicating the stream of filters it has been run through.
  76.      */
  77.     public void setProperties(Hashtable props) {
  78.     Object o = props.get("filters");
  79.     if (o == null) {
  80.         props.put("filters", toString());
  81.     } else if (o instanceof String) {
  82.         props.put("filters", ((String) o)+toString());
  83.     }
  84.     consumer.setProperties(props);
  85.     }
  86.  
  87.     /**
  88.      * Filter the information provided in the setColorModel method
  89.      * of the ImageConsumer interface.
  90.      * @see ImageConsumer#setColorModel
  91.      */
  92.     public void setColorModel(ColorModel model) {
  93.     consumer.setColorModel(model);
  94.     }
  95.  
  96.     /**
  97.      * Filters the information provided in the setHints method
  98.      * of the ImageConsumer interface.
  99.      * @see ImageConsumer#setHints
  100.      */
  101.     public void setHints(int hints) {
  102.     consumer.setHints(hints);
  103.     }
  104.  
  105.     /**
  106.      * Filters the information provided in the setPixels method of the
  107.      * ImageConsumer interface which takes an array of bytes.
  108.      * @see ImageConsumer#setPixels
  109.      */
  110.     public void setPixels(int x, int y, int w, int h,
  111.               ColorModel model, byte pixels[], int off,
  112.               int scansize) {
  113.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  114.     }
  115.  
  116.     /**
  117.      * Filters the information provided in the setPixels method of the
  118.      * ImageConsumer interface which takes an array of integers.
  119.      * @see ImageConsumer#setPixels
  120.      */
  121.     public void setPixels(int x, int y, int w, int h,
  122.               ColorModel model, int pixels[], int off,
  123.               int scansize) {
  124.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  125.     }
  126.  
  127.     /**
  128.      * Filters the information provided in the imageComplete method of
  129.      * the ImageConsumer interface.
  130.      * @see ImageConsumer#imageComplete
  131.      */
  132.     public void imageComplete(int status) {
  133.     consumer.imageComplete(status);
  134.     }
  135.  
  136.     /**
  137.      * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  138.      * of the pixel data from an ImageConsumer.
  139.      * The ImageFilter can respond to this request in one of three ways.
  140.      * <ol>
  141.      * <li>If the filter can determine that it will forward the pixels in
  142.      * TDLR order if its upstream producer object sends them
  143.      * in TDLR order, then the request is automatically forwarded by
  144.      * default to the indicated ImageProducer using this filter as the
  145.      * requesting ImageConsumer, so no override is necessary.
  146.      * <li>If the filter can resend the pixels in the right order on its
  147.      * own (presumably because the generated pixels have been saved in
  148.      * some sort of buffer), then it can override this method and
  149.      * simply resend the pixels in TDLR order as specified in the
  150.      * ImageProducer API.  <li>If the filter simply returns from this
  151.      * method then the request will be ignored and no resend will
  152.      * occur.  </ol> @see ImageProducer#requestTopDownLeftRightResend
  153.      * @param ip The ImageProducer that is feeding this instance of
  154.      * the filter - also the ImageProducer that the request should be
  155.      * forwarded to if necessary.
  156.      */
  157.     public void resendTopDownLeftRight(ImageProducer ip) {
  158.     ip.requestTopDownLeftRightResend(this);
  159.     }
  160.     
  161.     /**
  162.      * Clones this object.
  163.      */
  164.     public Object clone() { 
  165.     try { 
  166.         return super.clone();
  167.     } catch (CloneNotSupportedException e) { 
  168.         // this shouldn't happen, since we are Cloneable
  169.         throw new InternalError();
  170.     }
  171.     }
  172. }
  173.